/**
 * @details: 
 * @brief: 
 * @version: 1.0
 * @author: liuri
 * @Date: 2021-10-05 03:53:47
 * @LastEditors: liuri
 * @LastEditTime: 2021-10-05 07:03:21
 * @FilePath: /TestProject/cPath/vtysh/cmd_element.h
 * @Copyright (C) 2021 liuri. All rights reserved.
 */
#ifndef __CMD_ELEMENT_H__
#define __CMD_ELEMENT_H__

#include <stdint.h>
#include "vector.h"
#include "vty.h"

struct cmd_element ;
typedef int (*element_proc) (struct cmd_element *, struct vty *, int, const char *argv[]);

struct cmd_element 
{
    const char *string;		/* Command specification by string. */
    element_proc  func;
    const char *doc;		/* Documentation of this command. */
    int daemon;             /* Daemon to which this command belong. */
    vector tokens;		    /* Vector of cmd_tokens */
    uint32_t attr;			/* Command attributes */
};



/**
 * @brief 定义节点
 */
#define DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attrs, dnum) \
  struct cmd_element cmdname = \
  { \
    .string = cmdstr, \
    .func = funcname, \
    .doc = helpstr, \
    .attr = attrs, \
    .daemon = dnum, \
  };

/**
 * @brief 声明函数
 */
#define DEFUN_CMD_FUNC_DECL(funcname) \
  static int funcname (struct cmd_element *, struct vty *, int, const char *[]);

/**
 * @brief 定义函数
 */
#define DEFUN_CMD_FUNC_TEXT(funcname) \
  static int funcname \
    (struct cmd_element *self __attribute__ ((unused)), \
     struct vty *vty __attribute__ ((unused)), \
     int argc __attribute__ ((unused)), \
     const char *argv[] __attribute__ ((unused)) )

/**
 * @brief 定义节点和函数
 */
#define DEFUN(funcname, cmdname, cmdstr, helpstr) \
	DEFUN_CMD_FUNC_DECL(funcname) \
	DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0) \
	DEFUN_CMD_FUNC_TEXT(funcname)

/**
 * @brief 带属性的定义节点
 */
#define DEFUN_ATTR(funcname, cmdname, cmdstr, helpstr, attr) \
  DEFUN_CMD_FUNC_DECL(funcname) \
  DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, attr, 0) \
  DEFUN_CMD_FUNC_TEXT(funcname)

/**
 * @brief 定义隐藏节点
 */
#define DEFUN_HIDDEN(funcname, cmdname, cmdstr, helpstr) \
  DEFUN_ATTR (funcname, cmdname, cmdstr, helpstr, CMD_ATTR_HIDDEN)

/**
 * @brief 定义别名节点
 */
#define ALIAS(funcname, cmdname, cmdstr, helpstr) \
  DEFUN_CMD_ELEMENT(funcname, cmdname, cmdstr, helpstr, 0, 0)


void cmd_element_dump(struct cmd_element *cmd);

void install_element (vector cmd_vec, struct cmd_element *cmd);

void install_default_basic (vector cmd_vec);


#endif
